<script src="../../router/index.js"></script>
<template>
  <el-row >

      <el-col>
        <el-button type="text" style="font-size: 18px" @click="loadChefSelect(chef.id)">个人信息</el-button>
        <el-button type="text" style="font-size: 18px" @click="onOrder()">我的订单</el-button>
        <el-button type="text" style="font-size: 18px" @click="onWallet()" >钱包</el-button>
      </el-col>


    <el-col v-if="isShow1">
      <h4 >个人信息</h4>
        <el-divider>职业照片</el-divider>
        <img :src="chef.imgUrl" width="200px" height="144" >
        <el-divider>姓名</el-divider>
        <p>{{chef.name}}</p>
        <el-divider>手机号</el-divider>
        <p>{{chef.phoneNumber}}</p>
        <el-divider>年龄:</el-divider>
        <p>{{chef.age}}</p>
        <el-divider>性别:</el-divider>
        <p>{{chef.gender}}</p>
        <el-divider>密码:</el-divider>
        <p>{{chef.password}}</p>
        <el-divider>个人介绍:</el-divider>
        <p>{{chef.description}}</p>
        <el-divider>可预约时间:</el-divider>
        <p>{{chef.appointment}}</p>
        <el-divider></el-divider>
        <el-button type="primary" icon="el-icon-edit" circle size="mini"
                 @click="openEditDialog(chef)"></el-button>

      <!-- 修改数据的表单 -->
      <el-dialog title="修改厨师信息的表单" :visible.sync="editFormVisible">
        <el-form :model="editForm" :rules="editRules" ref="editForm" label-width="120px">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="editForm.name" ></el-input>
          </el-form-item>

          <el-form-item label="手机号" prop="phoneNumber">
            <el-input v-model="editForm.phoneNumber"></el-input>
          </el-form-item>

          <el-form-item label="密码" prop="password">
            <el-input v-model="editForm.password"></el-input>
          </el-form-item>

          <el-form-item label="年龄" prop="age">
            <el-input v-model="editForm.age"></el-input>
          </el-form-item>

          <!--修改图片    -->
          <el-form-item label="封面图" prop="imgUrl">
            <div class="picture-preview" v-if="editForm.imgUrl">
              <el-image class="image"
                        :src="editForm.imgUrl"
                        fit="cover"></el-image>
            </div>
            <el-upload
                class="picture-uploader"
                action="http://localhost:9523/resources/upload/image/chef/"
                :show-file-list="false"
                :on-success="handleUploadSuccess">
              <i class="el-icon-upload picture-uploader-icon"></i>
              <div class="picture-uploader-text">仅允许上传 <i>JPG</i> / <i>PNG</i> 格式的图片</div>
              <div class="picture-uploader-text">仅允许上传不超过 <i>5MB</i> 的图片</div>
            </el-upload>

          </el-form-item>

          <el-form-item label="厨师介绍" prop="description">
            <el-input v-model="editForm.description"></el-input>
          </el-form-item>

          <el-form-item label="可预约时间" prop="appointment">
            <el-input v-model="editForm.appointment"></el-input>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="editFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleEdit('editForm')">确 定</el-button>
        </div>
      </el-dialog>


    </el-col>

    <el-col v-if="isShow2">
      <h4>我的订单</h4>
    </el-col>

    <el-col style="align-content: center" v-if="isShow3">
      <div id="app" class="container mt-4">
        <div class="wallet">
          <h1 style="color: white">我的钱包</h1>
          <div class="balance" >
            {{ chef.balance }}
          </div>
          <h4 style="color: white"> 余额 (元)</h4>
          <input type="number" v-model="input" placeholder="请输入提现金额">
          <button class="btn btn-danger" @click="withdraw(chef)">提现</button>
        </div>
      </div>
    </el-col>
  </el-row>
</template>

<script>
export default {
  data(){
    return{
      isShow1: true,
      isShow2:false,
      isShow3:false,
      input:'',
      chef:{
      },
      // 编辑对话框相关数据
      editFormVisible: false,
      editForm: {
        id: '',
        name: '',
        phoneNumber: '',
        password: '',
        age: '',
        gender: '',
        imgUrl: '',
        description: '',
        appointment: '',
        displayState: '',

      },
      //编辑表单规则
      editRules: {
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'},
          {pattern: /^[a-zA-Z\u4e00-\u9fa5]{2,10}$/, message: '姓名必须是2~10长度的字符组成，且不允许使用标点符号', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输密码', trigger: 'blur'},
          {min: 6, max: 16, message: '密码必须是6~16位长度,允许使用数字,子母,标点符号', trigger: 'blur'}
        ],
        age: [
          {required: true, message: '请输入年龄', trigger: 'blur'},
          {pattern: /^(1[89]|[2-5][0-9]|6[0-5])$/, message: '年龄必须是18~65岁之间', trigger: 'blur'}
        ],
        imgUrl: [
          {required: true, message: '请上传图像', trigger: 'blur'},
          {pattern: /./, message: '请上正确格式', trigger: 'blur'}
        ],
        description: [
          {required: true, message: '请编写预约时间', trigger: 'blur'},
          {pattern: /./, message: '请写入正确格式', trigger: 'blur'}
        ],
        appointment: [
          {required: true, message: '请填写可预约时间', trigger: 'blur'},
          {pattern: /./, message: '请写入正确格式', trigger: 'blur'}
        ],
      },
      // 编辑表单的label宽度
      formLabelWidth: '120px'
    }
  },
  methods:{
    // 上传成功后的回调
    handleUploadSuccess(response) {
      console.log("------------------------------------------------"+response);
      this.editForm.imgUrl = response.data.url;
    },

    //弹出修改对话框
    openEditDialog(tableItem) {
      let url = 'http://localhost:9523/chef/' + tableItem.id;
      console.log('url = ' + url);

      this.axios
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.editForm = jsonResult.data;
          this.editFormVisible = true;
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
              this.loadChefSelect();
            }
          });
        }
      });
    },

    // 执行修改
    handleEdit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let url = 'http://localhost:9523/chef/' + this.editForm.id + '/update/info';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.editForm);
          console.log('formData = ' + formData);

          this.axios
              .post(url, formData).then((response) => {
            let jsonResult = response.data;
            if (jsonResult.state == 20000) {
              this.$message({
                message: '修改厨师成功！',
                type: 'success'
              });
              this.editFormVisible = false;
              this.loadChefSelect();
            } else if (jsonResult.state == 40400) {
              let title = '操作失败';
              this.$alert(jsonResult.message, title, {
                confirmButtonText: '确定',
                callback: action => {
                  this.editFormVisible = false;
                  this.loadChefSelect();
                }
              });
            } else {
              let title = '操作失败';
              this.$alert(jsonResult.message, title, {
                confirmButtonText: '确定',
                callback: action => {
                }
              });
            }
          });
        }
      });
    },

    //余额提现
    withdraw(chef) {
      console.log("*************"+this.input)
      if (this.input > this.chef.balance) {
        alert("余额不足");
      } else {
        this.chef.balance = this.chef.balance-this.input;
        console.log("*************"+chef.balance)
        // 发送提现请求，更新服务器上的余额
        let url = 'http://localhost:9523/chef/tx/' +chef.id+","+this.chef.balance ;
        console.log('url = ' + url);

        this.axios
            .get(url).then((response) => {
          let jsonResult = response.data;
          if (jsonResult.state == 20000) {
            //this.chef=jsonResult.data;
          } else {
            let title = '操作失败';
            this.$alert(jsonResult.message, title, {
              confirmButtonText: '确定',
              callback: action => {
                this.loadChefSelect();
              }
            });
          }
        });

        // 清空输入框
        this.input = 0;
      }
    },

    //显示订单
    onOrder(){
      this.isShow1=false;
      this.isShow2=true;
      this.isShow3=false;
    },
    //显示钱包
    onWallet(){
      this.isShow1=false;
      this.isShow2=false;
      this.isShow3=true;
    },
    //显示厨师个人信息
    loadChefSelect(id) {
      this.isShow1=true;
      this.isShow2=false;
      this.isShow3=false;
      id = this.$route.query.id;
      console.log("-------------"+id)
      let url = 'http://localhost:9523/chef/' + id;
      console.log('url = ' + url);

      this.axios
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.chef = jsonResult.data;
          console.log("**********************"+this.chef.balance)
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
              this.loadChefSelect();
            }
          });
        }
      });
    }
  },
  mounted() {
    this.loadChefSelect();
  }
}
</script>

<style>
.wallet {
  background-color: deepskyblue;
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
  border: 1px solid #ccc;
  border-radius: 5px;
  text-align: center;
}

.balance {
  color: white;
  font-size: 24px;
  margin-top: 20px;
  margin-bottom: 10px;
}

input[type="number"] {
  padding: 10px;
  margin-bottom: 10px;
  border: 1px solid #ccc;
  border-radius: 5px;
  width: 350px;

}

button {
  padding: 10px 20px;
  border-radius: 5px;
  cursor: pointer;
  font-size: 16px;
  margin-top: 10px;
}

button.btn-danger {
  background-color: #dc3545;
  color: #fff;
}

button.btn-success {
  background-color: #28a745;
  color: #fff;
}

.picture-uploader {
  float: left;
  width: 240px;
  height: 135px;
  margin-right: 20px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.picture-uploader .picture-uploader-icon {
  width: 240px;
  height: 80px;
  font-size: 58px;
  color: #8c939d;
  line-height: 80px;
  text-align: center;
}

.picture-uploader .picture-uploader-text {
  width: 240px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  font-size: 10px;
  color: #8c939d;
}

.picture-uploader .picture-uploader-text i {
  font-size: 10px;
  font-weight: bold;
  font-style: normal;
  color: #F56C6C;
}

.picture-preview {
  width: 240px;
  height: 135px;
  margin-right: 20px;
  border: 1px solid #e8e6e6;
  border-radius: 6px;
  float: left;
  display: inline;
  position: relative;
}

.picture-preview .image {
  width: 240px;
  height: 135px;
  border-radius: 6px;
}

</style>
